/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package schoolLink;
 
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

/**
 *
 * @author TRUNG
 */
public class HTTPconnect {
    private HTTPInterface myInterface;

    public void setInterface(HTTPInterface i){
        myInterface = i;
    }

    public void getData(String link){
        link = link.trim();
        myInterface.startConnect();
        HttpConnection c = null;
        InputStream is = null;
        StringBuffer sb = new StringBuffer();
        try {
            c = (HttpConnection)Connector.open(link,Connector.READ_WRITE, true);
            c.setRequestMethod(HttpConnection.GET); //default
            is = c.openInputStream(); // transition to connected!

            int chr;
            while ((chr = is.read()) != -1){
                sb.append((char) chr);
            }
        }catch (IOException x){           
            myInterface.httpError(HTTPInterface.ERROR_CONNECT_INTERNET_FAIL);
            //x.printStackTrace();
        }finally{
            try{
                is.close();
                c.close();
            } catch (IOException x){
                myInterface.httpError(HTTPInterface.ERROR_CLOSE_CONNECTION_FAIL);
                //x.printStackTrace();               
            }
        }
        myInterface.stopConnect(sb.toString()); 
    }
}
